import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const Tips 				= $Lazy.load(()=>import('#tp/tips'))
const TableComponent	= $Lazy.state(()=>import('./cpt/table-component'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '标本箱编号',			name: 'box_number' },
			{ label: '标本箱条码号',			name: 'box_code' },
			{ label: '收单时间',		names: ['start_date','end_date'], type:'date-range' },
		],
		isDisabled:true,
		formData:[
			{ label: '姓名', 		name:'name', 		full:true,		p:' ',			disabled:true },
			{ label: '用户名', 		name:'account', 	full:true,							required:true,	onBlur:(value)=>{this.getName(value)}},
			{ label: '密码', 		name:'password', 	full:true, 		type:'password',	required:true,	onBlur:(value)=>{this.getName(value)}},
			{ label: '接收数量', 	name:'num', 		full:true, 		onBlur: (value)=>{
				if(value !== this.state.selectedRow.spec_num){
					$fn.msg.error('接收数量与标本数量不符，请确认')
				}
			} },
			{ label: '开箱温度', 	name:'warm', 		full:true, 		},
		]
	}
	
	submit = {}
	
	componentDidMount(){
		this.fetch()
	}
		
	// paging
	fetch = () => $fn.fetch.call(this,'box/reclist')
	// 根据账号获取姓名
	getName = (value)=>{
		if(value){
			const formValue = this.formRef.getValue(['account','password'])
			$http.submit(null, 'employee/getuserrealname', {param:{phone:formValue.account,password:formValue.password},loading:false }).then((data)=> {
				if(!$fn.hasArray(data)){
					if(data.length === 0){
						this.formRef.setValue({name:''})
					}else{
						this.formRef.setValue({name:data.real_name})
					}
				}
			})
		}
	} 
	
	// 表单
	cols = [
		{ title: '标本箱编号', 		dataIndex: 'box_number', 		...$fn.table.exp_num, },
		{ title: '标本箱条码号', 	dataIndex: 'box_code', 			...$fn.table.exp_num, },
		{ title: '收单人', 			dataIndex: 'receiver_name', 	width:150 },
		{ title: '条码数量', 		dataIndex: 'spec_count', 		width:100 },
		{ title: '标本数量', 		dataIndex: 'spec_num', 			width:100 },
		{ title: '到达时间', 		dataIndex: '', 					...$fn.table.time },
		{ title: '收单时间', 		dataIndex: 'receive_time', 		...$fn.table.time },
	]
	// 操作按钮
	controls = () => [
		Controls.click.call(this, '确认接收', (keys,rows) => {
			this.submit.uuid = keys[keys.length - 1]
			const userData = $fn.getUser()
			const { empl_name, real_name } = userData
			const m = $fn.longSave('remember')
			if($fn.hasObject(m)){
				for(var i in m){ m[i] = Encrypt.decode(m[i]) }
			}
			this.formRef.setValue({account: empl_name, password: m?m.password:'', name:real_name })
			this.modal.open({formRef: this.formRef})
		}, { isKeys:1, code:'F1'} ),
	]
	render(){
		return (
			<>
				<Page title='接收标本箱' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this, {type: 1,
						onRow: (rows) => {
							this.setState({isDisabled:false})
							this.setState({selectedRow:rows})
							this.tableRef.fetch(rows)
						}
					} ) }
					{/* 子表格 */}
					<TableComponent onRef={ ref => this.tableRef = ref }/>
				</Page>
				{/* 确认接收弹框 */}
				{FormModal.call(this,{ title:'确认接收', labelWidth:'s5', width:600, api:'box/receive',
					prefix: (<Tips text='请输入用户名与密码进行接收确认' className = 'mb5' />),
				})}
			</>
		)
	}
}

export default Index